<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWInputManager
 *
 * @author jianfeng
 */
class BWInputMediaManager extends BEntity {

    public $owner;
    //put your code here
    protected $inputMediaList = array();
    private $isLoaded = false;
    public $resourceId;
    public $resourceType;
    public $inputMediaId;
    public $inputMediaGroupId;
    public $spaceId;

    public function __construct($owner) {
        if ($owner instanceof BEntity) {
            $this->owner = $owner;
            $this->resourceType = get_class($owner);
            $this->resourceId = $owner->getId();
        }
    }

    //put your code here

    public function addInputMedia($inputMediaData, $data = NULL) {

        $inputMedia = NULL;

        if ($inputMediaData instanceof BWInputMedia) {
            $inputMedia = $inputMediaData;
            $inputMedia->save();
        } else {
            $inputMedia = new BWInputMedia();
            $inputMedia->save($inputMediaData);
        }

        if ($inputMedia->getId() != NULL) {

            $iMediaId = $inputMedia->getId();
            $row = $this->getRepository()->getRow(array(
                'resourceId' => $this->resourceId,
                'resourceType' => $this->resourceType,
                'input_id' => $iMediaId,
                'spaceId' => Blyn::app()->getActiveSpace()->getId()
            ));
            if (!$row instanceof BRow || $row->getId() == NULL) {//not find in table
                $inputMedia->save();
                $newManager = clone $this;
                $newManager->inputMediaId = $inputMedia->getId();
                $newManager->spaceId = Blyn::app()->getActiveSpace()->getId();
                $newManager->save();
            }
        }
    }

    public function removeInputMedia($inputMediaData) {
        if ($inputMediaData instanceof BWInputMedia) {
            $inputMedia = $inputMediaData;
            if ($inputMedia->getId() != NULL) {
                $iMediaId = $inputMedia->getId();
                $row = $this->getRepository()->getRow(array(
                    'resourceId' => $this->resourceId,
                    'resourceType' => $this->resourceType,
                    'input_id' => $iMediaId,
                    'spaceId' => Blyn::app()->getActiveSpace()->getId()
                ));
                if ($row != NULL) {//if find row
                    $row->attributes['status'] = BStatus::Status_Object_Deleted;
                    $row->save();
                }
            }
        }
    }

    /**
     * this function use to find input media
     * @param type $mediaContext, can be name or with other conditions for find input media
     * @param type $data
     */
    public function getInputMedia($mediaContext = array(), $data = NULL) {
        if ($this->inputMediaList == NULL || empty($this->inputMediaList))
            $this->loadInputMediaList();

        $mediaList = $this->inputMediaList;

        foreach ($mediaList as $id => $media) {
            if (is_string($mediaContext)) {
                $name = $mediaContext;
                if ($media->getName() == $name)
                    return $media;
            }
            if (is_array($mediaContext)) {
                if ($media instanceof BEntity) {
                    $propertyList = $media->getPropertyList();
                    $isSelected = TRUE;
                    foreach ($propertyList as $name => $value) {
                        if (isset($mediaContext[$name]) && $mediaContext[$name] != $value)
                            $isSelected = FALSE;
                    }
                    if ($isSelected)
                        return $media;
                }
            }
            if ($mediaContext instanceof BWInputMedia) {
                $cProperList = $mediaContext->getPropertyList();
                $mProperList = $media->getPropertyList();
                $isSelected = TRUE;
                foreach ($mProperList as $name => $value) {
                    if (isset($cProperList[$name]) && $cProperList[$name] != $value)
                        $isSelected = FALSE;
                }
                if ($isSelected)
                    return $media;
            }
        }
    }

    public function getInputMediaList($mediaContext = array(), $data = NULL) {
        if ($this->inputMediaList == NULL)
            $this->loadInputMediaList();

        $mediaList = $this->inputMediaList;
        if ($mediaContext == NULL)
            return $mediaList;
        else {
            $list = array();

            foreach ($mediaList as $id => $media) {
                if (is_string($mediaContext)) {
                    $name = $mediaContext;
                    if ($media->getName() == $name)
                        $list[$media->getId()] = $media;
                }
                if (is_array($mediaContext)) {
                    if ($media instanceof BEntity) {
                        $propertyList = $media->getPropertyList();
                        $isSelected = TRUE;
                        foreach ($propertyList as $name => $value) {
                            if (isset($mediaContext[$name]) && $mediaContext[$name] != $value)
                                $isSelected = FALSE;
                        }
                        if ($isSelected)
                            $list[$media->getId()] = $media;
                    }
                }
                if ($mediaContext instanceof BWInputMedia) {
                    $cProperList = $mediaContext->getPropertyList();
                    $mProperList = $media->getPropertyList();
                    $isSelected = TRUE;
                    foreach ($mProperList as $name => $value) {
                        if (isset($cProperList[$name]) && $cProperList[$name] != $value)
                            $isSelected = FALSE;
                    }
                    if ($isSelected)
                        $list[$media->getId()] = $media;
                }
            }
            return $list;
        }
    }

    protected function loadInputMediaList($reload = FALSE, $data = NULL) {
        if ($reload || !$this->isLoaded) {
            $view = $this->getRepository()->getView();
            $rows = $view->rows;

            $list = array();

            foreach ($rows as $row) {
                $inputMediaId = $row->attributes['input_id'];
                if (is_numeric($inputMediaId) && $inputMediaId > 0) {
                    $inputMedia = new BWInputMedia($inputMediaId);
                    //$inputMedia->load($inputMediaId);
                    $list[$inputMedia->getId()] = $inputMedia;
                }
            }

            $this->inputMediaList = $list;
            $this->isLoaded = TRUE;
        }
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->setModel('BWMResourceInput');
        $repository->addPropertyToColumnMap('inputMediaId', 'input_id');
        $repository->addPropertyToColumnMap('inputMediaGroupId', 'input_group_id');
        $repository->addPropertyToColumnMap('resourceId', 'resource_id');
        $repository->addPropertyToColumnMap('resourceType', 'resource_type');
        return $repository;
    }

    public function save($context = null) {
        $repository = $this->getRepository();
        return $repository->saveEntity($context, $this);
    }

}
